/*
 * SelectedFileListDisplayer.java
 *
 * Created on 14 Haziran 2008 Cumartesi, 21:07
 */
package com.pegaa.uploader.ui.selectedfilelist;

import com.pegaa.uploader.common.CustomGridLayout;
import com.pegaa.uploader.ui.fileselection.EmptyListMessage;
import com.pegaa.uploader.common.StackLayout;
import com.pegaa.uploader.config.ConfigHolder;
import com.pegaa.uploader.lang.Lang;
import com.pegaa.uploader.ui.filelist.item.ListItemUI;
import java.awt.event.ItemEvent;
import javax.swing.JPanel;

/**
 *
 * @author  tayfun
 */
public class SelectedFileList extends javax.swing.JPanel implements java.awt.event.ItemListener {

    private ConfigHolder configHolder = null;
    private StackLayout layout = null;
    private EmptyListMessage emptyList = null;
    private JPanel selectedFileList = null;
    private SelectedFileLister selectedFileLister = null;
    private DndHandler dndHandler = null;

    /** Creates new form SelectedFileListDisplayer */
    public SelectedFileList() {
        initComponents();
        updateComponents();
    }

    public void setConfigHolder(ConfigHolder configHolder) {
        //this.listItemUIs = new ArrayList<ListItemUI>(20);
        this.configHolder = configHolder;
        updateStrings();
        initDND();
    }

    /**
     * Drag and drop support
     */
    private void initDND() {
        dndHandler = new DndHandler();
        dndHandler.setConfigHolder(configHolder);

        dndHandler.addDropTarget(this);
        dndHandler.addDropTarget(this.emptyList);
    }

    public void setListModel(SelectedFileListModel model) {
        this.selectedFileLister = new SelectedFileLister(this.configHolder, model);
        this.selectedFileLister.addFileSelectionListeners(this);
    }

    private void updateComponents() {
        this.layout = new StackLayout();
        this.selectedFileListMainPanel.setLayout(this.layout);

        this.emptyList = new EmptyListMessage();
        this.selectedFileListMainPanel.add(this.emptyList);

        this.selectedFileList = new JPanel();
        this.selectedFileList.setLayout(new CustomGridLayout(150, 125));
        this.selectedFileListMainPanel.add(this.selectedFileList);
        
        this.layout.showComponent(this.emptyList, this.selectedFileListMainPanel);
    }

    private void updateStrings() {
        Lang lang = (Lang) this.configHolder.getObject("global.lang");
        this.emptyList.setText(lang.get("selectedfilelist.emptymessage"), "");
    }

    /**
     *  ListItem event listener.
     * 
     * @param e
     */
    public void itemStateChanged(ItemEvent e) {
        ListItemUI itemUI = (ListItemUI) e.getItem();

        if (e.getID() == SelectedFileLister.EVENT_ADD) {
            this.selectedFileList.add(itemUI);
        } else {
            this.selectedFileList.remove(itemUI);
        }

        this.layout.showComponent(this.selectedFileList, this.selectedFileListMainPanel);
        this.selectedFileList.revalidate();
        this.selectedFileList.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        selectedFileListMainPanel = new javax.swing.JPanel();

        setLayout(new java.awt.GridLayout(1, 0));

        org.jdesktop.layout.GroupLayout selectedFileListMainPanelLayout = new org.jdesktop.layout.GroupLayout(selectedFileListMainPanel);
        selectedFileListMainPanel.setLayout(selectedFileListMainPanelLayout);
        selectedFileListMainPanelLayout.setHorizontalGroup(
            selectedFileListMainPanelLayout.createParallelGroup(1)
            //.addGap(0, 460, Short.MAX_VALUE)
        );
        selectedFileListMainPanelLayout.setVerticalGroup(
            selectedFileListMainPanelLayout.createParallelGroup(1)
            //.addGap(0, 321, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(selectedFileListMainPanel);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel selectedFileListMainPanel;
    // End of variables declaration//GEN-END:variables


}
